#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import opengate as gate
import opengate.contrib.spect.ge_discovery_nm670 as spect_ge_nm670
from opengate.contrib.phantoms import nemaiec
from spect_helpers import add_177lu_sphere_sources
from exercices_helpers import *

if __name__ == "__main__":
    # create the simulation
    sim = gate.Simulation()

    # main options
    sim.g4_verbose = False
    # sim.visu = True
    sim.visu_type = "vrml"
    # sim.visu_type = "qt"
    sim.number_of_threads = 6
    sim.random_seed = "auto"
    sim.progress_bar = True
    sim.output_dir = "./output"

    # units
    m = gate.g4_units.m
    sec = gate.g4_units.second
    days = 3600 * 24 * sec
    cm = gate.g4_units.cm
    mm = gate.g4_units.mm
    nm = gate.g4_units.nm
    MeV = gate.g4_units.MeV
    keV = gate.g4_units.keV
    Bq = gate.g4_units.Bq
    kBq = 1000 * Bq
    MBq = 1000 * kBq
    cm3 = gate.g4_units.cm3
    Bq = gate.g4_units.Bq
    BqmL = Bq / cm3

    # world size
    sim.world.size = [2 * m, 2 * m, 2 * m]
    sim.world.material = "G4_AIR"

    # IEC phantom
    nemaiec.add_iec_phantom(sim, "iec")

    # spect head (debug mode = only a small part of the collimator is simulation, for visu mode)
    spect, _, _ = spect_ge_nm670.add_spect_head(sim, "spect")
    spect.translation = [0, 0, -28 * cm]

    # detector input plane (+ 1nm to avoid overlap)
    pos, crystal_dist, psd = spect_ge_nm670.get_plane_position_and_distance_to_crystal(
        "lehr"
    )
    pos += 1 * nm
    print(f"plane position     {pos / mm} mm")
    print(f"crystal distance   {crystal_dist / mm} mm")
    # detector input plane
    detector_plane = sim.add_volume("Box", "det_plane")
    detector_plane.mother = spect.name
    detector_plane.size = [57.6 * cm, 44.6 * cm, 1 * nm]
    detector_plane.translation = [0, 0, pos]
    detector_plane.material = "G4_Galactic"
    detector_plane.color = [0, 1, 0, 1]

    # ARF detector model
    arf = sim.add_actor("ARFActor", "arf")
    arf.attached_to = detector_plane.name
    arf.output_filename = "projection4.mhd"
    arf.batch_size = 2e5
    arf.image_size = [128, 128]
    arf.image_spacing = [4.41806 * mm, 4.41806 * mm]
    arf.verbose_batch = True
    arf.distance_to_crystal = crystal_dist
    print(crystal_dist)
    # arf.distance_to_crystal = 74.625 * mm
    arf.pth_filename = Path("data") / "arf_lu177.pth"
    # this adds a slide that contains all hits (for debug)
    arf.enable_hit_slice = True

    # Lu177 source (only the gammas) in all spheres
    add_177lu_sphere_sources(sim, activity=1e4 * BqmL)

    # add stat actor
    stats = sim.add_actor("SimulationStatisticsActor", "stats")
    stats.track_types_flag = True
    stats.output_filename = "stats4.txt"

    # phys
    sim.physics_manager.physics_list_name = "G4EmStandardPhysics_option3"
    sim.physics_manager.set_production_cut("world", "all", 1 * mm)

    # ---------------------------------------------------------------------
    # start simulation
    # sim.running_verbose_level = gate.EVENT
    sim.run_timing_intervals = [[0, 100 * sec]]
    sim.run()

    # print stats
    print(stats)
